package com.waitingsnow.modules.giftPlay.view
{
import com.waitingsnow.core.debug.Logger;
import com.waitingsnow.core.model.config.AppConfig;
import com.waitingsnow.core.model.vo.GiftVO;
import com.waitingsnow.core.util.ds.ExternalUtil;
import com.waitingsnow.modules.giftPlay.GiftPlayFacade;
import com.waitingsnow.modules.giftPlay.model.GiftPlayProxy;
import com.waitingsnow.modules.giftPlay.view.baseView.NormalGiftPLayView;
import com.waitingsnow.modules.giftPlay.view.baseView.SpecialGiftPlayView;

import flash.display.Sprite;
import flash.events.Event;

/**
 * 
 * @author waitingsnow
 * $Id: GiftPlayView.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class GiftPlayView extends Sprite
{
	//==========================================================================
	//  Class variables
	//==========================================================================
	static public const NAME:String = "GiftPlayView";
	
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>GiftPlayView</code> instance.
	 * 
	 */
	public function GiftPlayView()
	{
		super();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var giftPlayProxy:GiftPlayProxy;
	private var curNormalGiftVO:GiftVO;
	private var curSpecialGiftVO:GiftVO;
	private var normalGiftPlay:NormalGiftPLayView;
	private var specialGiftPlay:SpecialGiftPlayView;
	
	//==========================================================================
	//  Methods
	//==========================================================================
	public function initDisplay():void
	{
		giftPlayProxy = GiftPlayFacade.getInstance().getContent(GiftPlayProxy.NAME) as GiftPlayProxy;
		ExternalUtil.callback(AppConfig.PLAY_GIFT_TO_SWF, playGiftHandler);
		normalGiftPlay = new NormalGiftPLayView();
		normalGiftPlay.x = 200;
		normalGiftPlay.y = 80;
		normalGiftPlay.addEventListener(Event.COMPLETE, giftPlayCompleteHandler);
		addChild(normalGiftPlay);
		/*
		playGiftHandler(2, 66, 1);
		playGiftHandler(3, 1314, 1);
		playGiftHandler(4, 999, 1);
		playGiftHandler(5, 288, 1);
		playGiftHandler(2, 288, 1);
		playGiftHandler(3, 588, 1);
		playGiftHandler(4, 999, 1);
		playGiftHandler(5, 288, 1);
		playGiftHandler(1, 1, 2);
		playGiftHandler(1, 1, 2);
		playGiftHandler(1, 1, 2);*/
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function playGiftHandler(giftID:uint, count:uint, type:int = 1):void
	{
		Logger.log("收到礼物id: " + giftID + " 礼物数量：" + count + " 礼物类型: " + type);
		var giftVO:GiftVO = giftPlayProxy.getGiftVOByID(giftID);
		giftVO.count = count;
		if (giftVO.giftType == 1)
		{
			giftPlayProxy.pushGift(giftVO);
			playQueueGift();
		}
		else
		{
			playSpecialGift(giftVO);
		}
	}
	
	private function playQueueGift():void
	{
		if (!normalGiftPlay.isRun)
		{
			curNormalGiftVO = giftPlayProxy.getCurGift();
			curNormalGiftVO && normalGiftPlay.playGift(curNormalGiftVO);
		}
	}
	
	private function playSpecialGift(giftVO:GiftVO):void
	{
		if (!specialGiftPlay)
		{
			specialGiftPlay = new SpecialGiftPlayView();
			specialGiftPlay.addEventListener(Event.COMPLETE, specialGiftPlayHandler);
			addChild(specialGiftPlay);
		}
		specialGiftPlay.playGift(giftVO);
	}
	
	private function checkClose():Boolean
	{
		var result:Boolean;
		if (!giftPlayProxy.playGiftArr.length)
		{
			if (specialGiftPlay && specialGiftPlay.runCount)
			{
				result = false;
			}
			else
			{
				Logger.log("call CLOSE_GIFT_TO_JS");
				ExternalUtil.call(AppConfig.CLOSE_GIFT_TO_JS);
				result = true;
			}
		}
		return result;
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function giftPlayCompleteHandler(event:Event):void
	{
		!checkClose() && playQueueGift();
	}
	
	private function specialGiftPlayHandler(event:Event):void
	{
		checkClose();
	}
}
}